package gui.commands;

import java.util.Stack;

import common.Result;

public class CommandHistory
{
  private Stack<Command> undoCommands = new Stack<Command>();
  private Stack<Command> redoCommands = new Stack<Command>();

  /**
   * @param command
   *          The most recently executed <code>Command</code> to add.
   * @return A <code>Result</code> indicating whether the {@link Command} was successfully added.
   */
  public Result add(Command command)
  {
    undoCommands.push(command);
    redoCommands.clear();
    return new Result(true);
  }

  /**
   * @return Whether there are {@link Command}s in the history that can be undone.
   */
  public boolean canUndo()
  {
    return !undoCommands.isEmpty();
  }

  /**
   * @return Whether there are {@link Command}s in the history that can be redone.
   */
  public boolean canRedo()
  {
    return !redoCommands.isEmpty();
  }

  /**
   * Removes the most recent {@link Command} from the history and calls its <code>reverse()</code>
   * method.
   */
  public void undo()
  {
    Command command = undoCommands.pop();
    command.reverse();
    redoCommands.push(command);
  }

  /**
   * Readds the most recently undone {@link Command} to the history and calls its
   * <code>execute()</code> method.
   */
  public void redo()
  {
    Command command = redoCommands.pop();
    command.execute();
    undoCommands.push(command);
  }
}
